package com.yongbao.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yongbao.common.PageInfo;
import com.yongbao.common.domain.BaseEntity;
import com.yongbao.common.utils.AssertUtil;
import com.yongbao.mapper.AreaStreetMapper;
import com.yongbao.model.domain.AreaStreetDO;
import com.yongbao.model.dtomapper.AreaStreetDtoMapper;
import com.yongbao.model.request.AreaStreetRequest;
import com.yongbao.model.util.AreaStreetConvert;
import com.yongbao.model.vo.AreaStreetTreeVO;
import com.yongbao.model.vo.AreaStreetVO;
import com.yongbao.service.AreaStreetService;
import com.yongbao.utils.TreeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhanghong
 */
@Service
public class AreaStreetServiceImpl extends ServiceImpl<AreaStreetMapper, AreaStreetDO> implements AreaStreetService {
    @Autowired
    private AreaStreetMapper areaStreetMapper;

    @Override
    public AreaStreetVO getByKey(long id) {
        AreaStreetVO areaStreetVO = AreaStreetDtoMapper.INSTANCE.do2VO(getById(id));
        return areaStreetVO;
    }

    @Override
    public PageInfo<AreaStreetVO> pageQuery(AreaStreetRequest request) {
        long count = areaStreetMapper.pageCount(request);
        List<AreaStreetDO> list = areaStreetMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<AreaStreetVO> areaStreetVOList = AreaStreetConvert.buildAreaStreetVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), areaStreetVOList, count);
    }

    @Override
    public List<AreaStreetTreeVO> listQuery(AreaStreetRequest request) {
        List<AreaStreetDO> list = areaStreetMapper.selectDOList(AreaStreetDtoMapper.INSTANCE.request2DO(request));
        List<AreaStreetTreeVO> areaStreetTreeVOS = AreaStreetConvert.buildAreaStreetTreeVoList(list);
        return (List<AreaStreetTreeVO>) TreeUtils.streamToTree(areaStreetTreeVOS, 0L);
    }

    @Override
    public Boolean add(AreaStreetRequest request) {
        save(AreaStreetDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    public Boolean updateByKey(AreaStreetRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(AreaStreetDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Transactional
    @Override
    public Boolean delByKey(String ids) {
        Long[] idList = Convert.toLongArray(ids);
        areaStreetMapper.deleteDOByIds(idList);
        List<AreaStreetDO> list = lambdaQuery().in(BaseEntity::getId, idList).eq(AreaStreetDO::getParentId, 0L).list();
        if (CollectionUtils.isNotEmpty(list)) {
            // 如果是跟节点一并删除根节点下的所有子节点
            List<Long> rootNodeIdList = list.stream().map(BaseEntity::getId).collect(Collectors.toList());
            lambdaUpdate().in(AreaStreetDO::getParentId, rootNodeIdList).remove();
        }
        return true;
    }
}
